$(function(){
    obj.init();
});

var obj = new function(){
    'use strict';
    return {
        dataGrid: null,
        dom: {
            // type: null,
            search: null,
            year: null,
            province: null,
            city: null,
            depart: null
        },
        params: {
            year: null
        },
        init: function () {
            this.dom.search = $('#bt_list_search');
            // this.dom.type       = $('#list_type');
            this.dom.year   = $('#list_year');
            this.dom.depart = $('#list_depart');
            this.dom.province = $('#select_province');
            this.dom.city = $('#select_city');

            this.dataGrid = new xhDataGrid($('#list'), 'cost/getChargeStats');
            this.dataGrid.parseToolbar();

            this.listener();
        },
        listener: function () {
            var $this = this;
            var $length = 0;
            $('#select_province').combobox({
                onSelect:function (record) {
                    if(record.value != '') {
                        $('#select_city').combobox({
                            url: baseUrl + 'customer/customer/getAreaById/' + record.value,
                            valueField: 'areaCode',
                            textField: 'areaName',
                            onSelect: function (record2) {
                                if(record2.areaCode != ''){
                                    $('#select_county').combobox({
                                        url: baseUrl + 'customer/customer/getAreaById/' + record2.areaCode,
                                        valueField: 'areaCode',
                                        textField: 'areaName'
                                    })
                                }else {
                                    $('#select_county').combobox('setValue','');
                                }
                            }
                        })
                    }else {
                        $('#select_city').combobox('setValue','');
                        $('#select_county').combobox('setValue','');
                    }
                }
            });
            //运营区域
            $('#list_depart').combotree({
                url: '/Device/getDepData',
                animate:true,
                onLoadSuccess:function(node,data){
                    $length = data.length;
                    $.each(data,function (id,value) {
                        $length = $length + value.children.length;
                    })
                },
                onShowPanel:function(){
                    if ($length <= 8){
                        $(this).combobox('panel').height("auto");//高度自适应
                    }else{
                        $(this).combobox('panel').height("200");//高度自定义
                    }
                },
                onBeforeSelect: function (node) {
                    var t = $(this).tree;
                    var isLeaf = t('isLeaf', node.target);
                    if (!isLeaf) {//选择的不是叶子节点
                        return false;
                    }
                }
            });
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            }).click();
            xhTool.bindDocKeyDown($this.dom.search);
            //跳转
            // $('#bt_list_jump').click(function () {
            //     if (xhExtra.buttonDisabled($(this))) return false;
            //     $this.funObj().jumpStudCharge();
            // });
            //导出
            $('#bt_list_export').click(function () {
                $this.funObj().exportSchoolCharge();
            });
        },
        funObj: function(){
            var $this = this;
            return {
                search: function () {
                    $this.params.year = xhExtra.getValue($this.dom.year);
                    // $this.params.part = xhExtra.getValue($this.dom.part);
                    // $this.params.province = xhExtra.getValue($this.dom.province);
                    // $this.params.city = xhExtra.getValue($this.dom.city);
                    $this.params.province = xhExtra.getValue($this.dom.province);
                    $this.params.city = xhExtra.getValue($this.dom.city);
                    $this.params.depart = xhExtra.getValue($this.dom.depart);
                    $this.dataGrid.reload($this.params);
                },
                exportSchoolCharge: function () {
                    $this.funObj().search();
                    window.location.href = baseUrl + 'cost/exportChargeStats?' + xhTool.parseParam($this.params);
                }
                // jumpStudCharge: function () {
                //     var params = {
                //         schoolId: $this.dataGrid.getSelectValue('iSchoolId'),
                //         schoolName: encodeURI($this.dataGrid.getSelectValue('sSchoolName')),
                //         year: xhExtra.getValue($this.dom.year),
                //         grade: $this.dataGrid.getSelectValue('grade').replace('级', '')
                //     };
                //     window.top.xhTop.createNewTab('收费管理', 'cost/studCharge?' + xhTool.parseParam(params));
                // }
                // exportSchoolCharge: function () {
                //     $this.funObj().search();
                //     window.location.href = baseUrl + 'cost/exportSchoolCharge?' + xhTool.parseParam($this.params);
                // }
            }
        }
    }
}

